package st.business.tepth.business.user.busnumber.workholder.repair.work.content;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.RepairTaskBean;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleAdapter;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroublePagerAdapter;
import st.shadow.library.ui.indicator.magicindicator.MagicIndicator;
import st.shadow.library.ui.indicator.magicindicator.ViewPagerHelper;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.CommonNavigator;

/**
 * @author zhangyu
 * @date 2019/8/2
 * 报修故障与诊断的代理处理
 */
public class TroubleUiDelegate {
    private Context mContext;
    private MagicIndicator mMagicIndicator;
    private ViewPager mViewPagerTrouble;
    private TextView mTvTroubleCount;
    private TroublePagerAdapter mTroublePagerAdapter;
    private ViewPager.OnPageChangeListener mOnPageChangeListener;
    //常见诊断变更监听
    private OnItemChooseListener<ArrayList<DiagnoseBean>> mCommonDiagnoseChangeListener;
    //删除本地诊断监听
    private OnItemChooseListener<DiagnoseBean> mDeleteLocalListener;
    //删除服务器诊断监听
    private OnItemChooseListener<DiagnoseBean> mDeleteServiceListener;
    //点击关联方案监听
    private OnItemChooseListener<DiagnoseBean> mContactListener;
    //点击添加方案监听
    private OnItemChooseListener<DiagnoseBean> mOnAddProgramListener;

    public void setDeleteLocalListener(OnItemChooseListener<DiagnoseBean>
                                               deleteLocalListener) {
        mDeleteLocalListener = deleteLocalListener;
    }

    public void setDeleteServiceListener(OnItemChooseListener<DiagnoseBean>
                                                 deleteServiceListener) {
        mDeleteServiceListener = deleteServiceListener;
    }

    public void setContactListener(OnItemChooseListener<DiagnoseBean> contactListener) {
        mContactListener = contactListener;
    }

    public void setOnAddProgramLisener(OnItemChooseListener<DiagnoseBean>
                                               onAddProgramLisener) {
        mOnAddProgramListener = onAddProgramLisener;
    }

    public TroubleUiDelegate(
            Context context,
            MagicIndicator magicIndicator,
            ViewPager viewPagerTrouble,
            TextView tvTroubleCount) {
        mContext = context;
        mMagicIndicator = magicIndicator;
        mViewPagerTrouble = viewPagerTrouble;
        mTvTroubleCount = tvTroubleCount;
        ViewPagerHelper.bind(mMagicIndicator, mViewPagerTrouble);
    }

    public void setOnCommonDiagnoseChangeListener(OnItemChooseListener<ArrayList<DiagnoseBean>>
                                                          commonDiagnoseChangeListener) {
        mCommonDiagnoseChangeListener = commonDiagnoseChangeListener;
    }

    public void setData(final ArrayList<RepairTaskBean.FaultPhenomenonListBean> list
            , int status) {
        if (list == null || list.isEmpty()) {
            mTvTroubleCount.setVisibility(View.GONE);
        } else {
            mTvTroubleCount.setVisibility(View.VISIBLE);
            mTvTroubleCount.setText(String.format(Locale.CHINA, "%d/%d",
                    1, list.size()));
            mViewPagerTrouble.setAdapter(mTroublePagerAdapter =
                    new TroublePagerAdapter(mContext, status,
                            list, new OnItemChooseListener<DiagnoseBean>() {
                        @Override
                        public void onChoose(DiagnoseBean diagnoseBean) {
                            //删除本地诊断
                            if (mDeleteLocalListener != null) {
                                mDeleteLocalListener.onChoose(diagnoseBean);
                            }
                        }
                    }, new OnItemChooseListener<DiagnoseBean>() {
                        @Override
                        public void onChoose(final DiagnoseBean diagnoseBean) {
                            //删除服务器诊断
                            if (mDeleteServiceListener != null) {
                                mDeleteServiceListener.onChoose(diagnoseBean);
                            }
                        }
                    }, new OnItemChooseListener<DiagnoseBean>() {
                        @Override
                        public void onChoose(DiagnoseBean diagnoseBean) {
                            // 关联方案
                            if (mContactListener != null) {
                                mContactListener.onChoose(diagnoseBean);
                            }
                        }
                    }, new OnItemChooseListener<DiagnoseBean>() {
                        @Override
                        public void onChoose(DiagnoseBean diagnoseBean) {
                            //添加方案
                            if (mOnAddProgramListener != null) {
                                mOnAddProgramListener.onChoose(diagnoseBean);
                            }
                        }
                    }));
            if (mOnPageChangeListener != null) {
                mViewPagerTrouble.removeOnPageChangeListener(mOnPageChangeListener);
            }
            mViewPagerTrouble.addOnPageChangeListener(
                    mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {

                        }

                        @Override
                        public void onPageSelected(int i) {
                            mTvTroubleCount.setText(String.format(Locale.CHINA, "%d/%d",
                                    i + 1, list.size()));
                            RepairTaskBean.FaultPhenomenonListBean faultPhenomenonListBean = list.get(i);
                            if (mCommonDiagnoseChangeListener != null) {
                                mCommonDiagnoseChangeListener.onChoose(faultPhenomenonListBean
                                        .getCommonFaultDiagnosticList());
                            }
                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {

                        }
                    });
            CommonNavigator commonNavigator = new CommonNavigator(mContext);
            commonNavigator.setAdapter(new TroubleAdapter(list,
                    new TroubleAdapter.OnItemChooseListener() {
                        @Override
                        public void onItemChoose(int index, int count) {
                            mViewPagerTrouble.setCurrentItem(index);
                            mTvTroubleCount.setText(String.format(Locale.CHINA, "%d/%d",
                                    index + 1, count));
                            RepairTaskBean.FaultPhenomenonListBean faultPhenomenonListBean =
                                    list.get(index);
                            if (mCommonDiagnoseChangeListener != null) {
                                mCommonDiagnoseChangeListener.onChoose(faultPhenomenonListBean
                                        .getCommonFaultDiagnosticList());
                            }
                        }
                    }));
            mMagicIndicator.setNavigator(commonNavigator);
            if (!list.isEmpty()) {
                if (mCommonDiagnoseChangeListener != null) {
                    mCommonDiagnoseChangeListener.onChoose(list.get(0)
                            .getCommonFaultDiagnosticList());
                }
            }
        }
    }
}
